//main class
function SplitTheBill(){
	var that = this;
	this.start = function(){
		this.view = new SplitTheBillRenderer();
		this.repo = new SplitTheBillRepo();
		this.buildDisplay();
	};
	
	this.buildDisplay = function(){
		this.view.init(this.getBodyId(), this.getUniqueIdent());
		this.buildBillActions();
		this.buildBill();
	};
	
	this.buildBillActions = function(){
		this.view.renderBillActions(this.view.getMainPart(this.view.idents.billActionsCont).elem, {
			actions: [
				{
					label: 'order',
					ident: 'add-item-link',
					callback: that.showAddItem			
				},
			
				{
					label: 'person',
					ident: 'add-person-link',
					callback: that.showAddPerson				
				}
				
			],
			
			context: that
		});
	};
	
	this.showAddPerson = function(params){
		//make defaulting possible here
		this.view.makeAddPersonForm(this.view.getMainPart(this.view.idents.billAddPerson).elem, {
			orderers : this.repo.getOrderers(),
			context: that
		});
		this.view.showAddPerson({
			add: that.addPerson,
			context: that
		});
	};
	
	this.addPerson = function(vals){
		var newOrderer = new Orderer();
		newOrderer.setName(vals.name);
		this.repo.addOrderer(newOrderer);
		this.showAddPerson();
	};
	
	//optional parameters for sending a default value
	this.showAddItem = function(params){
		//make defaulting possible here
		this.view.makeAddItemForm(this.view.getMainPart(this.view.idents.billAddItem).elem, {
			orderers : this.repo.getOrderers(),
			context: that
		});
		this.view.showAddItem({
			add: that.addItem,
			context: that
		});
		
	};
	
	this.addItem = function(vals){
		var newItem = new OrderItem();
		newItem.setName(vals.name);
		newItem.setPrice(vals.price);
		newItem.setQty(vals.qty);
		this.repo.addOrderedItem(newItem);
		//this.repo.removeOrderedItem(newItem.ident);
		var totalPrice = newItem.price * newItem.qty;
		
		this.view.addBillRow({
			ident: newItem.ident,
			rowItems: [
				{
					ident: '',
					content: newItem.name 
				},
				
				{
					ident: '',
					content: newItem.price 
				},
				
				{
					ident: '',
					content: newItem.qty
				},
				
				{
					ident: '',
					content: totalPrice 
				}
			]
		});
		//this.view.addBillRow(['test', 'test', 'test', 'test', 'test']);
	};
	
	this.buildBill = function(){
		this.view.renderBill(this.view.getMainPart(this.view.idents.billCont).elem, {
			//Bill headers for table
			headers: [
				{
					content: 'food ordered',
					ident: '',
					cssClass: ''
				},
				
				{
					content: 'price',
					ident: '',
					cssClass: ''
				},
				
				{
					content: 'quantity',
					ident: '',
					cssClass: ''
				},
				
				{
					content: 'total cost',
					ident: '',
					cssClass: ''
				},
				
				{
					content: 'actions',
					ident: '',
					cssClass: ''
				}
				
			]
		});
	};
	
}
//inherit from the main Engine class
SplitTheBill.prototype = new Engine();